package merlin.dao;

import java.util.*;
import java.sql.*;

import merlin.beans.*;
import merlin.utils.*;

public class ShareholderDetailsDAO implements java.io.Serializable{

	Connection con;

	public ShareholderDetailsDAO(Connection con){
		this.con = con;
	}

	public DbResultSet selectShareholderDetailsRecordsByCompanyNumber(String companyNumber)throws SQLException{

		ArrayList records = new ArrayList();
		PreparedStatement pstmt = con.prepareStatement("SELECT COMPANY_NUMBER,SHAREHOLDER,SHAREHOLDING FROM SHAREHOLDER_DETAILS WHERE COMPANY_NUMBER = ?");
		pstmt.setString(1,companyNumber);

		ResultSet rs =  pstmt.executeQuery();
		while(rs.next()){
			String holding = MerlinUtilities.capitalizeFully(rs.getString("SHAREHOLDING")); // capitalize.
			holding = holding.replaceAll("Gbp", "GBP");	// convert distorted GBP back to original.
			ShareholderDetailsBean bn = new ShareholderDetailsBean(rs.getString("COMPANY_NUMBER"),MerlinUtilities.capitalizeFully(rs.getString("SHAREHOLDER")), holding);
			records.add(bn);
		}
		rs.close();
		pstmt.close();
		if(!records.isEmpty())return new DbResultSet(records,records.size()); else return new DbResultSet();
	}
}